package authoritah.ui.event;

import java.sql.Timestamp;
import java.util.ArrayList;

import javax.swing.ComboBoxModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import com.toedter.calendar.IDateEditor;

import authoritah.db.Attendance;
import authoritah.db.Event;
import authoritah.db.EventType;
import authoritah.utils.TimeUtils;

public class EventAttendanceModel {

	private EventAttendanceTableModel inputTableModel;
	// TODO fix up all the documents.
	private Document eventNameDocument;
	private Document startDocument;
	private Document endDocument;
	private Document maxPointsDocument;
	private Document copyPasteDocument;
	private ComboBoxModel eventTypeModel;
	private IDateEditor dateEditor;

	public void reset() {
		try {
			inputTableModel.setRowCount(0);
			eventNameDocument.remove(0, eventNameDocument.getLength());
			startDocument.remove(0, startDocument.getLength());
			endDocument.remove(0, endDocument.getLength());
			maxPointsDocument.remove(0, maxPointsDocument.getLength());
			copyPasteDocument.remove(0, copyPasteDocument.getLength());
			eventTypeModel.setSelectedItem(eventTypeModel.getElementAt(0));
			dateEditor.setDate(null);
		} catch (BadLocationException e) {
			e.printStackTrace();
			// shouldn't happen
		}
	}

	public EventAttendanceModel() {
		inputTableModel = new EventAttendanceTableModel();
	}

	public void setEventNameDocument(Document eventNameDocument) {
		this.eventNameDocument = eventNameDocument;
	}

	public void setStartDocument(Document startDocument) {
		this.startDocument = startDocument;
	}

	public void setEndDocument(Document endDocument) {
		this.endDocument = endDocument;
	}

	public void setMaxPointsDocument(Document maxPointsDocument) {
		this.maxPointsDocument = maxPointsDocument;
	}

	public void setCopyPasteDocument(Document copyPasteDocument) {
		this.copyPasteDocument = copyPasteDocument;
	}

	public EventAttendanceTableModel getInputTableModel() {
		return inputTableModel;
	}

	public Document getEventNameDocument() {
		return eventNameDocument;
	}

	public Document getStartDocument() {
		return startDocument;
	}

	public Document getEndDocument() {
		return endDocument;
	}

	public Document getMaxPointsDocument() {
		return maxPointsDocument;
	}

	public Document getCopyPasteDocument() {
		return copyPasteDocument;
	}

	public ComboBoxModel getEventTypeModel() {
		return eventTypeModel;
	}

	public void setEventTypeModel(ComboBoxModel eventTypeModel) {
		this.eventTypeModel = eventTypeModel;
	}

	public IDateEditor getDateEditor() {
		return dateEditor;
	}

	public void setDateEditor(IDateEditor dateEditor) {
		this.dateEditor = dateEditor;
	}

	public Timestamp getStart() {
		Timestamp returnStamp = null;
		try {
			returnStamp = TimeUtils.parseTime(dateEditor.getDate(),
					startDocument.getText(0, startDocument.getLength()));
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		return returnStamp;
	}

	public Timestamp getEnd() {
		Timestamp returnStamp = null;
		try {
			returnStamp = TimeUtils.parseTime(dateEditor.getDate(), endDocument
					.getText(0, endDocument.getLength()));
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		return returnStamp;
	}

	public int getMaxPoints() {
		int returnValue = 0;
		try {
			returnValue = Integer.parseInt(maxPointsDocument.getText(0,
					maxPointsDocument.getLength()));
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		return returnValue;
	}

	public ArrayList<Attendance> getAttendanceList() {
		return inputTableModel.getAttendanceList();
	}

	public EventType getSelectedEventType() {
		return (EventType) eventTypeModel.getSelectedItem();
	}

	public String getEventName() {
		String returnValue = "";
		try {
			returnValue = eventNameDocument.getText(0, eventNameDocument
					.getLength());
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		return returnValue;
	}
}
